﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Validation;

	public class UserPreference : EntityBase<UserPreference>
	{
		private Country _country;
		private TimeZone _timeZone;

		/// <summary>
		/// Initializes a new instance of the <see cref="UserPreference"/> class.
		/// </summary>
		public UserPreference()
		{
			DeterminePermission(Roles.Users);
		}

		/// <summary>
		/// Gets or sets the country id.
		/// </summary>
		/// <value>The country id.</value>
		public virtual int CountryId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the time zone id.
		/// </summary>
		/// <value>The time zone id.</value>
		public virtual int TimeZoneId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the messages.
		/// </summary>
		/// <value>The messages.</value>
		public virtual int Messages
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is messaging enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is messaging enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsMessagingEnabled
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is messaging retricted enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is messaging retricted enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsMessagingRetrictedEnabled
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is messaging notification enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is messaging notification enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsMessagingNotificationEnabled
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is profile messaging enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is profile messaging enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsProfileMessagingEnabled
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is profile messaging retricted enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is profile messaging retricted enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsProfileMessagingRetrictedEnabled
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is profile messaging notification enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is profile messaging notification enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsProfileMessagingNotificationEnabled
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is friendship notification enabled.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is friendship notification enabled; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsFriendshipNotificationEnabled
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the DST.
		/// </summary>
		/// <value>The DST.</value>
		public virtual UserPreferenceDaylightSavingTime Dst
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the start of week.
		/// </summary>
		/// <value>The start of week.</value>
		public virtual UserPreferenceStartOfWeek StartOfWeek
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the time format.
		/// </summary>
		/// <value>The time format.</value>
		public virtual UserPreferenceTimeFormat TimeFormat
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the country.
		/// </summary>
		/// <value>The country.</value>
		public virtual Country Country
		{
			get
			{
				return this._country;
			}
			set
			{
				this._country = value;

				if (this._country != null)
				{
					this.CountryId = this._country.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the time zone.
		/// </summary>
		/// <value>The time zone.</value>
		public virtual TimeZone TimeZone
		{
			get
			{
				return this._timeZone;
			}
			set
			{
				this._timeZone = value;

				if (this._timeZone != null)
				{
					this.TimeZoneId = this._timeZone.Id;
				}
			}
		}

		/// <summary>
		/// Updates the messages.
		/// </summary>
		public virtual void UpdateMessages()
		{
			UpdateMessages(1, true);
		}

		/// <summary>
		/// Updates the messages.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="addition">if set to <c>true</c> [addition].</param>
		public virtual void UpdateMessages(int value, bool addition)
		{
			if (addition)
			{
				this.Messages += value;
			}
			else
			{
				this.Messages -= value;
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserPreference> Rules()
		{
			return base.Rules();
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}